class MissionRegistrationsController < ApplicationController
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :create, :update ],
         :redirect_to => { :controller => 'command', :action => 'index' }  

  before_filter :authorize_admin, :only => [ :index, :list, :show ]

  def index
    list
    render :action => 'list'
  end

  def list
    @mission_registration_pages, @mission_registrations = paginate :mission_registrations, :per_page => 10
  end

  def show
    @mission_registration = MissionRegistration.find(params[:id])
  end

  def new
    @mission_registration = MissionRegistration.new
    @member = Member.find(session[:member_id])
    session[:mission_id] = params[:id]
  end

  def create
    @mission_registration = MissionRegistration.new(params[:mission_registration])
    @mission_registration.member = Member.find(session[:member_id])
    @mission_registration.character = Character.find(@mission_registration.character_id)
    mission = Mission.find(session[:mission_id])
    session[:mission_id] = nil
    mission.mission_registrations << @mission_registration
    @mission_registration.character.mission_registrations << @mission_registration
    @mission_registration.member.mission_registrations << @mission_registration
    if @mission_registration.save and mission.save
      flash[:notice] = 'MissionRegistration was successfully created.'
      redirect_to :controller => 'command', :action => 'index'
    else
      render :action => 'new'
    end
  end

  def edit
    @mission_registration = MissionRegistration.find(params[:id])
  end

  def update
    @mission_registration = MissionRegistration.find(params[:id])
    if @mission_registration.update_attributes(params[:mission_registration])
      flash[:notice] = 'MissionRegistration was successfully updated.'
      redirect_to :action => 'show', :id => @mission_registration
    else
      render :action => 'edit'
    end
  end

  def destroy
    MissionRegistration.find(params[:id]).destroy
    redirect_to :controller => 'command', :action => 'index'
  end
end
